package com.aw.baselib.util.update

import android.content.Context
import com.aw.baselib.R
import com.aw.baselib.base.AwBaseApplication
import com.aw.baselib.util.AwAppUtils
import com.aw.baselib.util.AwLog
import com.aw.baselib.util.AwVersionUtil
import com.google.gson.Gson
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Request
import okhttp3.Response
import java.io.IOException

/**
 * @author : hzw
 * @date : 2021/6/4
 */
object AwUpdateCheckUtil {
    const val URL_UPDATE =
        "https://sirun-app.oss-cn-beijing.aliyuncs.com/Weima/android/demoNew/version.json"

    fun checkUpdate(context: Context?, callback: IUpdateCheckCallback?) {
        if (context == null || callback == null) {
            return
        }
        val builder = Okhttp3Manager.okhttp
        val mOkHttpClient = builder.build()
        val request =
            Request.Builder() //                .cacheControl(CacheControl.FORCE_NETWORK) //取消缓存读取
                .method("GET", null)
                .url(URL_UPDATE)
                .build()
        val mcall = mOkHttpClient.newCall(request)
        mcall.enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                AwLog.d("checkUpdate onFailure: " + e.message)
                callback.checkFail(
                    AwBaseApplication.instance?.getString(R.string.update_hint_check_fail)
                )
            }

            @Throws(IOException::class)
            override fun onResponse(call: Call, response: Response) {
                if (null != response.cacheResponse()) {
                    AwLog.d(
                        "checkUpdate onResponse cacheResponse: " + response.cacheResponse()
                            .toString()
                    )
                } else {
                    val body = response.body()!!.string()
                    AwLog.d(
                        "checkUpdate onResponse networkResponse: " + response.networkResponse()
                            .toString() + " ,body: " + body
                    )
                    val bean = Gson().fromJson(body, UpdateBean::class.java)
                    if (bean != null && AwAppUtils.getAppVersionInfo(
                            context,
                            AwAppUtils.TYPE_VERSION.TYPE_VERSION_NAME
                        )?.let {
                            bean.appVersion?.let { it1 ->
                                AwVersionUtil.compareVersions(
                                    it, it1
                                )
                            }
                        }!! > 0
                    ) {
                        AwLog.d("checkUpdate onResponse networkResponse 需升级")
                        callback.checkSuccess(bean)
                    } else {
                        AwLog.d("checkUpdate onResponse networkResponse 不需升级")
                        callback.checkFail(
                            AwBaseApplication.instance?.getString(R.string.update_hint_newest)
                        )
                    }
                }
            }
        })
    }
}